package com.activititest.controller;


import com.activititest.appentity.Response;
import com.activititest.appentity.View_Statistics_list;
import com.activititest.entity.*;
import com.activititest.service.*;
import com.activititest.service.impl.AdminACServerImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

//
@RestController
@RequestMapping("/useradmin")
public class UserAdminController {
    @Autowired
    private AdminACServerImpl adminACServerImpl;
    @Autowired
    private IEmTaskCmdPolltantService emTaskCmdPolltantService;
    @Autowired
    private IEmEnterpriseService emEnterpriseService;
    @Autowired
    private  IEmTaskCmdFormService emTaskCmdFormService;
    @Autowired
    private IVEnterpriseNumService iVEnterpriseNumService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private IVTaskCommitFromService IVTaskCommitFromService;
    @RequestMapping(value = "test",method = RequestMethod.GET)
    public String getTest(){
        return "hello word";
    }
//    //发布任务
    @PostMapping("/publishtask/")
    public String publishTask(@RequestBody ParameterCmd parameterCmd) throws ParseException {
        //enterprise_id
        //pollutant_ids
//       Gson gson = new Gson();
////        ParameterCmd user = gson.fromJson(parameterstr, ParameterCmd.class);
//        System.out.println(parameterCmd.getEnterprise_id()+"----------------------");
        System.out.println("------------------------");
        System.out.println(parameterCmd);
        System.out.println("------------------------");
        System.out.println(parameterCmd.getEnterprise_id());
        EmEnterprise emEnterprise = emEnterpriseService.getById(parameterCmd.getEnterprise_id());
//        //emEnterpriseService.selectByMap(new HashMap(){{put("enterprise_id",parameterCmd.getEnterprise_id());}});
       // String taskid=adminACServerImpl.publishTask(emEnterprise.getEnterpriseAreaId().toString(),parameterCmd.getPollutant_ids());
        String taskid=adminACServerImpl.publishTask(parameterCmd.getCity_id(),parameterCmd.getPollutant_ids());
       EmTaskCmdPolltant emTaskCmdPolltant;
        EmTaskCmdForm emTaskCmdForm = new EmTaskCmdForm();

        Date date = new Date();//获得系统时间.
//        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
//        String nowTime = sdf.format(date);
 //       Date time = sdf.parse( nowTime );
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
         instant.atZone(zoneId).toLocalDateTime();
        emTaskCmdForm.setTaskId(taskid);
        emTaskCmdForm.setCmdFormDatetime( instant.atZone(zoneId).toLocalDateTime());
       emTaskCmdForm.setEnterpriseId(Integer.parseInt(parameterCmd.getEnterprise_id()));
        emTaskCmdFormService.save(emTaskCmdForm);
        System.out.println(parameterCmd.getPollutant_ids().size()+"ppnum---");
        for (String ppid:parameterCmd.getPollutant_ids()){
            emTaskCmdPolltant=new EmTaskCmdPolltant();
            emTaskCmdPolltant.setTaskId(taskid);
            emTaskCmdPolltant.setPollutantId(Integer.parseInt(ppid));
            emTaskCmdPolltantService.save(emTaskCmdPolltant);
        }
        return "yes";
    }
    @PostMapping("/erpriselist/")
    public Response<List<View_Statistics_list>> erpriselist(){
        long startTime = System.currentTimeMillis(); //获取开始时间
        List<VEnterpriseNum> vEnterpriseNums = iVEnterpriseNumService.list();
        List<View_Statistics_list> view_statistics_lists=new ArrayList();
        View_Statistics_list view_statistics_list ;
        for(VEnterpriseNum vEnterpriseNum:vEnterpriseNums){

            view_statistics_list = new View_Statistics_list();
            TaskQuery taskQuery = taskService.createTaskQuery();
            int confirmnum = taskQuery.taskCandidateGroup(String.valueOf(vEnterpriseNum.getEnterpriseAreaId())).taskName("confirm").list().size();
            int runsendnum = taskQuery.taskCandidateGroup(String.valueOf(vEnterpriseNum.getEnterpriseAreaId())).taskName("runsend").list().size();
            int findshnum = historyService.createHistoricTaskInstanceQuery().taskCandidateGroup(String.valueOf(vEnterpriseNum.getEnterpriseAreaId())).taskName("runsend").finished().list().size();
            view_statistics_list.setCity_name(vEnterpriseNum.getEnterpriseType());
            view_statistics_list.setAreaId(vEnterpriseNum.getEnterpriseAreaId());
            view_statistics_list.setPollution_num(String.valueOf(vEnterpriseNum.getNum()));
            view_statistics_list.setCompleted_num(String.valueOf(findshnum));
            view_statistics_list.setTo_be_confirmed_num(String.valueOf(confirmnum));
            view_statistics_list.setTo_be_completed_num(String.valueOf(runsendnum));
            view_statistics_lists.add(view_statistics_list);
        }
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(view_statistics_lists);
        euserResponse.setMsg("yes");
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间


        return euserResponse;
    }
//    @PostMapping("/provincelist/")
//    public Response<View_Statistics_list> provincelist(){
//        long startTime = System.currentTimeMillis(); //获取开始时间
//        View_Statistics_list view_statistics_list = new View_Statistics_list();
//
//        view_statistics_list.setAreaId(210000);
//        view_statistics_list.setCity_name("辽宁省");
//        view_statistics_list.setPollution_num("1");
//        view_statistics_list.setTo_be_confirmed_num("1");
//        view_statistics_list.setTo_be_completed_num("1");
//        view_statistics_list.setCompleted_num("1");
//        Response euserResponse = new Response();
//        euserResponse.setCode(200);
//        euserResponse.setData(view_statistics_list);
//        euserResponse.setMsg("yes");
//        long endTime = System.currentTimeMillis(); //获取结束时间
//        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间
//        return euserResponse;
//    }
     //超级用户获取最新数据
      @PostMapping("/updatefinshtask/")
    public Response<List<VTaskCommitFrom>>  updatefinshtask(@RequestParam(value="datetime",required=false)String datetime){
        //2020-06-03 11:13:49
          System.out.println(datetime);
        QueryWrapper<VTaskCommitFrom> commit_form_datetime = new QueryWrapper<VTaskCommitFrom>().gt("commit_form_datetime", datetime);
        List<VTaskCommitFrom> list = IVTaskCommitFromService.list(commit_form_datetime);
        System.out.println(list.size());
          System.out.println("-------------------------------");
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

}
